﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _3S_Cam
{
    public partial class CamWebElement : UserControl
    {
        private string _ip;

        CamWebElement()
        {
            InitializeComponent();
        }

        public CamWebElement(string ip)  :this()
        {
            _ip = ip;
            camWeb.ScrollBarsEnabled = false;
            camWeb.ScriptErrorsSuppressed = true;
            Init();
        }

        void Init()
        {
            camWeb.Navigate("http://" + _ip);
            
            camWeb.DocumentCompleted += (sender, args) =>
                {
                    
                    //ProcessDocument(camWeb.Document);
                };
            camWeb.StatusTextChanged += (sender, args) =>
                {
                    if (camWeb.ReadyState != WebBrowserReadyState.Complete)
                        return;
                    ProcessDocument(camWeb.Document);
                };
        }

        void ProcessDocument(HtmlDocument doc)
        {
            var element = doc.GetElementById("CamViewerTd");
            
            if (element == null)
            {
                var f = doc.Window.Frames[0];
                var doc1 = f.Document;
                doc1.Body.Style = "overflow:hidden";
                element = doc1.GetElementById("CamViewerTd");
            }
            if (element == null)
                return;

            element.ScrollIntoView(true);

            camWeb.Height = element.ClientRectangle.Height;
            camWeb.Width = element.ClientRectangle.Width;
        }

    }
}
